package com.hc.java.threadpool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * submit不会阻塞
 *
 * @author huangchao E-mail:fengquan8866@163.com
 * @version 创建时间：2024/6/17 11:46
 */
public class 线程池submit {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService executor = new ThreadPoolExecutor(2, 2,
                5, TimeUnit.SECONDS, new LinkedBlockingQueue<>(20), r -> {
            Thread thread = new Thread(r);
            thread.setName("线程池invoke-" + thread.hashCode());
            return thread;
        });
        List<Callable<Integer>> list = new ArrayList<Callable<Integer>>() {{
            add(() -> {
                System.out.println("线程执行中------2");
                Thread.sleep(1000);
                System.out.println("线程执行完了------3");
                return 1;
            });
        }};
        Future<Integer> future = executor.submit(list.get(0));
        System.out.println("等待线程执行--------1");
        System.out.println("执行结果4：" + future.get());
        System.exit(0);
    }
}
